package io.github.syske.springbootjdbcmybatisdemo.controller;


import io.github.syske.springbootjdbcmybatisdemo.common.ResultObject;
import io.github.syske.springbootjdbcmybatisdemo.dao.UserDao;
import io.github.syske.springbootjdbcmybatisdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserDao userDao;

    @RequestMapping("/user/getUserByName")
    public Object getUserByName(@RequestParam String user_name){
        Map<String,Object> map = new HashMap<>();
        map.put("user_name",user_name);
        List<User> user = userDao.getUserByName(map);
        ResultObject resultObject = new ResultObject();
        resultObject.data = user;
        return resultObject;
    }

    @RequestMapping("/api/currentUser")
    public Object getCurrentUser(HttpSession session){
        User user = (User) session.getAttribute("user");
        ResultObject resultObject = new ResultObject();
        resultObject.data = user;
        return resultObject;
    }

    @RequestMapping("/api/updatePassword")
    public Object updatePassword(@RequestBody User user, HttpSession session){
        ResultObject resultObject =new ResultObject();
        String username = user.user_name;
        String password = user.user_password;
        String user_code = user.user_code;
        User u = new User();
        u.user_code = user_code;
        u.user_password = password;
        Integer integer = userDao.updatePasswordById(u);
        if(integer>0){
            resultObject.message = "密码更新成功";
            return resultObject;
        }
        resultObject.code=401;
        resultObject.message = "密码更新失败";
        return resultObject;
    }

    @RequestMapping("api/verifiedUser")
    public Object verifiedUser(@RequestBody Map<String,Object> param,HttpSession session){
        if(param.get("password")!=null && !param.get("password").equals("") && !param.get("password").equals("undefined")){
            User user = (User) session.getAttribute("user");
            if(user!=null){
                Map<String,Object> map = new HashMap<>();
                map.put("user_name",user.user_name);
                map.put("user_password",param.get("password").toString());
                List<User> list = userDao.getUserByName(map);
                if(list==null || list.size()==0){
                    ResultObject oo =new ResultObject();
                    oo.code=401;
                    oo.message="密码输入有误";
                    return oo;
                }else{ResultObject oo =new ResultObject();
                    oo.message="验证成功";
                    return oo;
                }
            }else {
                ResultObject oo =new ResultObject();
                oo.code=403;
                oo.message="session过期,请重新登录";
                return oo;
            }
        }else{
            ResultObject oo =new ResultObject();
            oo.code=401;
            oo.message="登录密码不能为空";
            return oo;
        }

    }
}
